#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "astar.h"
#include <QElapsedTimer>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->widget_goods_5->hide();
    ui->pushButton_pay->hide();
    QFont tmp;
    tmp.setPointSize(30);
    goods_flag=-1;
    ui->textBrowser->setFont(tmp);
    tmp.setPointSize(21);
    label_clear();
    ui->textBrowser_price_all->setFont(tmp);
    ui->pushButton_deal_1->setFont(tmp);
    ui->pushButton_deal_2->setFont(tmp);
    ui->pushButton_deal_3->setFont(tmp);
    ui->pushButton_deal_4->setFont(tmp);

    ui->pushButton_reduce_1->setFont(tmp);
    ui->pushButton_reduce_2->setFont(tmp);
    ui->pushButton_reduce_3->setFont(tmp);
    ui->pushButton_reduce_4->setFont(tmp);
    ui->textBrowser_goods_1->setFont(tmp);
    ui->textBrowser_goods_2->setFont(tmp);
    ui->textBrowser_goods_3->setFont(tmp);
    ui->textBrowser_goods_4->setFont(tmp);
    ui->pushButton_deal_all->setFont(tmp);
    tmp.setPointSize(40);
    ui->pushButton_start->setFont(tmp);

    p={
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},

        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},


      };

    pwm_l.Init(1,50000000,40000000,1);
    pwm_r.Init(2,50000000,40000000,1);


    goods_1.give_goods(1,12,4,12);
    goods_2.give_goods(2,13,7,12);
    goods_3.give_goods(3,14,7,8);
    goods_4.give_goods(4,15,4,8);

    gpio_l_1.Init_1(51,0);
    gpio_l_2.Init_1(50,0);
    gpio_r_1.Init_1(48,0);
    gpio_r_2.Init_1(49,0);
    c_00.creat_C(0,0,300);
    c_01.creat_C(0,600,200);
    c_02.creat_C(480,300,400);
    c_10.creat_C(0,0,300);
    c_11.creat_C(0,600,200);
    c_12.creat_C(480,300,400);
    direction_m=0;
    direction_t=1800;
    loc_aim_x=0,loc_aim_y=0;
    loc_now_x=0,loc_now_y=0;
    R_x=0;
    R_y=0;
    R1_x=0;
    R1_y=0;
    this->initSerialPort();

    this->initTimer();






}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::initSerialPort()
{

    QList<QSerialPortInfo>  infos = QSerialPortInfo::availablePorts();
    if(infos.isEmpty())
        {

            return ;
        }

        serial5.close();
        serial5.setPort(infos[5]);
        serial5.open(QIODevice::ReadWrite);          //读写打开
        serial5.setBaudRate(QSerialPort::Baud115200);  //波特率
        serial5.setDataBits(QSerialPort::Data8);     //数据位 ui->textEdit->append(QString::number(direction_m));
        serial5.setParity(QSerialPort::NoParity);    //无奇偶校验
        serial5.setStopBits(QSerialPort::OneStop);   //无停止位
        serial5.setFlowControl(QSerialPort::NoFlowControl);  //无控制
        serial4.close();
        serial4.setPort(infos[4]);
        serial4.open(QIODevice::ReadWrite);          //读写打开
        serial4.setBaudRate(QSerialPort::Baud115200);  //波特率
        serial4.setDataBits(QSerialPort::Data8);     //数据位
        serial4.setParity(QSerialPort::NoParity);    //无奇偶校验
        serial4.setStopBits(QSerialPort::OneStop);   //无停止位
        serial4.setFlowControl(QSerialPort::NoFlowControl);  //无控制
        serial0.close();
        serial0.setPort(infos[0]);
        serial0.open(QIODevice::ReadWrite);          //读写打开
        serial0.setBaudRate(QSerialPort::Baud115200);  //波特率
        serial0.setDataBits(QSerialPort::Data8);     //数据位
        serial0.setParity(QSerialPort::NoParity);    //无奇偶校验
        serial0.setStopBits(QSerialPort::OneStop);   //无停止位
        serial0.setFlowControl(QSerialPort::NoFlowControl);  //无控制

        serial3.setPort(infos[3]);
        serial3.open(QIODevice::ReadWrite);          //读写打开
        serial3.setBaudRate(QSerialPort::Baud115200);  //波特率
        serial3.setDataBits(QSerialPort::Data8);     //数据位
        serial3.setParity(QSerialPort::NoParity);    //无奇偶校验
        serial3.setStopBits(QSerialPort::OneStop);   //无停止位
        serial3.setFlowControl(QSerialPort::NoFlowControl);  //无控制
        connect(&serial5,SIGNAL(readyRead()),this,SLOT(serialRead5()));
        connect(&serial4,SIGNAL(readyRead()),this,SLOT(serialRead4()));
        connect(&serial0,SIGNAL(readyRead()),this,SLOT(serialRead0()));
        connect(&serial3,SIGNAL(readyRead()),this,SLOT(serialRead3()));

        serial4.write("AT+switchdis=1\r\n");
        serial5.write("AT+switchdis=1\r\n");

}





void MainWindow::serialRead5()
{

    QString qa;

    static QString te;

    qa = serial5.readAll();

    te.append(qa);
    if(te.contains('m'))
    {
       te=te.replace('a',"");
       te=te.replace('n',"");
       te=te.replace('m',"");
       te=te.replace(':',"");
       te=te.replace('.',"");


       if(te[0]=='0')
       {
           te=te.remove(0,1);
            c_00.r=te.toInt();
       }
       else if(te[0]=='1')
       {
           te=te.remove(0,1);
           c_01.r=te.toInt();
       }
       else if(te[0]=='2')
       {
           te=te.remove(0,1);
           c_02.r=te.toInt();
       }


       te.clear();

    }
    else if (te.contains('K'))
    {

        te.clear();
    }
}
void MainWindow::serialRead4()
    {
        QString qa;

        static QString te;

        qa = serial4.readAll();

        te.append(qa);
        if(te.contains('m'))
        {
           te=te.replace('a',"");
           te=te.replace('n',"");
           te=te.replace('m',"");
           te=te.replace(':',"");
           te=te.replace('.',"");

           if(te[0]=='0')
           {
               te=te.remove(0,1);
                c_10.r=te.toInt();
           }
           else if(te[0]=='1')
           {
               te=te.remove(0,1);
               c_11.r=te.toInt();
           }
           else if(te[0]=='2')
           {
               te=te.remove(0,1);
               c_12.r=te.toInt();
           }
//            ui->textBrowser_warning->append("my");
 //           ui->textBrowser_warning->append(te);
           te.clear();
        }
        else if (te.contains('K'))
        {

         te.clear();
        }



}
void MainWindow::serialRead3()  //STM32   ge shi :   a:90.0,02*
    {
        QString qa;
        int tmp_location;
        static QString te;
        static int tmp;
        static int i=0;
        qa = serial3.readAll();


        te.append(qa);
        if(te.contains('a')&&te.contains('*'))
        {

            if(te.size()<=12)
            {
            qa=te.right(2);
            qa=qa.replace("*","");
            tmp_data_flag=qa.toInt();
            tmp_location=te.indexOf("$");
            te=te.left(tmp_location);
            te=te.replace('a',"");
            te=te.replace('.',"");
            direction_m=te.toInt();
            if(direction_m==tmp)
                i++;
            else
            {
                i=0;
            }
            if(i>=3)
            {
                run_flag=1;
                i=0;
            }
            tmp=direction_m;

            switch (tmp_data_flag) {
            case 1:
                navigation_flag=1;
                loc_aim_x=goods_1.loc_x;
                loc_aim_y=goods_1.loc_y;
                break;
            case 2:
                navigation_flag=1;
                loc_aim_x=goods_2.loc_x;
                loc_aim_y=goods_2.loc_y;
                break;
            case 3:
                navigation_flag=1;
                loc_aim_x=goods_3.loc_x;
                loc_aim_y=goods_3.loc_y;
                break;
            case 4:
                navigation_flag=1;
                loc_aim_x=goods_4.loc_x;
                loc_aim_y=goods_4.loc_y;
                break;
            case 5:
                navigation_flag=0;

                break;
            default:
                break;

            }

            }

            te.clear();
        }


}
void MainWindow::serialRead0()
{

    QString qa;

    static QString te;

    qa = serial0.readAll();

    te.append(qa);


     if(te.contains('!'))
    {

       te=te.replace('!',"");

        if(goods_flag==-1)
        {
       if(te=="6956511921980")
       {
           goods_1.num++;
           goods_flag=10;

       }
       else if(te=="6922868281114")
       {
           goods_2.num++;
           goods_flag=10;

       }
       else if(te=="6956511933631")
       {
           goods_3.num++;
           goods_flag=10;

       }
       else if(te=="6957303867387")
       {
           goods_4.num++;
           goods_flag=10;
       }
        else
       {
           goods_num=-1;
       }
        }


       te.clear();
    }


}


void MainWindow::initTimer()
{
    m_pTimer = new QTimer(this);
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
    m_pTimer->start(100); // ms     1000 =1s
    motor_pTimer = new QTimer(this);
    connect(motor_pTimer, SIGNAL(timeout()), this, SLOT(motor_pwm()));
    motor_pTimer->start(20); // ms
}
void MainWindow::motor_pwm()
{

    int tmp_direction,pwm_value;
#if 0
    gpio_l_1.Value(1);
    gpio_l_2.Value(1);
    gpio_r_1.Value(1);
    gpio_r_2.Value(1);
#else
    /*
        ui->textBrowser->clear();
        ui->textBrowser->append(QString::number(direction_m));

        ui->textBrowser->append(QString::number(direction_t));

        ui->textBrowser->append(QString::number(loc_now_x));

        ui->textBrowser->append(QString::number(loc_now_y));
        */
        if(navigation_flag)
        {
                if(direction_t!=1800)
        {


        tmp_direction=direction_m-direction_t;

        pwm_value=abs((tmp_direction)*4000);

        if(tmp_direction>=(-50)&&tmp_direction<=50&&run_flag==0)
        {
            gpio_l_1.Value(0);
            gpio_l_2.Value(0);
            gpio_r_1.Value(0);
            gpio_r_2.Value(0);

        }
        else if(tmp_direction>=(-50)&&tmp_direction<=50&&run_flag==1)
        {
            pwm_l.Config(50000000,43500000);
            pwm_r.Config(50000000,43500000);
            gpio_l_1.Value(1);
            gpio_l_2.Value(0);
            gpio_r_1.Value(0);
            gpio_r_2.Value(1);

        }
        else
        {

            pwm_l.Config(50000000,40500000-pwm_value);
            pwm_r.Config(50000000,40500000-pwm_value);

            run_flag=0;
        if(tmp_direction>0)
        {
            gpio_l_1.Value(0);
            gpio_l_2.Value(1);
            gpio_r_1.Value(0);
            gpio_r_2.Value(1);
        }
        else
        {
            gpio_l_1.Value(1);
            gpio_l_2.Value(0);
            gpio_r_1.Value(1);
            gpio_r_2.Value(0);
        }
        }
        }

        else
        {


            tmp_direction=1800-abs(direction_m);

            pwm_value=abs((tmp_direction)*4000);

            if(tmp_direction<=50&&run_flag==0)
            {

                gpio_l_1.Value(0);
                gpio_l_2.Value(0);
                gpio_r_1.Value(0);
                gpio_r_2.Value(0);

            }
            else if(tmp_direction<=50&&run_flag==1)
            {
                pwm_l.Config(50000000,43500000);
                pwm_r.Config(50000000,43500000);
                gpio_l_1.Value(1);
                gpio_l_2.Value(0);
                gpio_r_1.Value(0);
                gpio_r_2.Value(1);
            }
            else
            {
                pwm_l.Config(50000000,40500000-pwm_value);
                pwm_r.Config(50000000,40500000-pwm_value);
                run_flag=0;

            if(direction_m<0)
            {

                gpio_l_1.Value(0);
                gpio_l_2.Value(1);
                gpio_r_1.Value(0);
                gpio_r_2.Value(1);
            }
            else
            {

                gpio_l_1.Value(1);
                gpio_l_2.Value(0);
                gpio_r_1.Value(1);
                gpio_r_2.Value(0);
            }
            }


        }
        }
        else {
            gpio_l_1.Value(0);
            gpio_l_2.Value(0);
            gpio_r_1.Value(0);
            gpio_r_2.Value(0);
        }

#endif
}
void MainWindow::handleTimeout()
{
     int price_all;

    MainWindow::getPoint0(c_01,c_00,c_02);
    MainWindow::getPoint1(c_11,c_10,c_12);
    this->loc_now_x=R1_x/30,this->loc_now_y=R1_y/30;
    if(goods_flag>=0)
    {
        goods_flag--;
    }

     if((abs(loc_aim_x-loc_now_x)+abs(loc_aim_y-loc_now_y))<=1)
    {
       navigation_flag=0;
    }

    if(loc_aim_x!=loc_now_x||loc_aim_y!=loc_now_y)
    {
        AStar *a = new AStar(16,20,p);
    Point start(this->loc_aim_x,this->loc_aim_y);  //实际的终点设置为起点
    Point end(this->loc_now_x,this->loc_now_y);
    Path_i=a->get_path(start,end);

    if(a->X_List[1]>a->X_List[0])
    {
        direction_t=-900;

    }
    else if(a->X_List[1]<a->X_List[0])
    {
        direction_t=900;

    }
    else if(a->Y_List[1]<a->Y_List[0])
    {
        direction_t=1800;

    }
    else
    {
       direction_t=0;
    }
    }



    if(goods_1.num)
    {
        ui->widget_goods_1->show();
        ui->textBrowser_goods_1->setText(QString::number(goods_1.num));

    }
    else
    {
      ui->widget_goods_1->hide();
    }
    if(goods_2.num)
    {
        ui->widget_goods_2->show();
        ui->textBrowser_goods_2->setText(QString::number(goods_2.num));

    }
    else
    {
      ui->widget_goods_2->hide();
    }
    if(goods_3.num)
    {
        ui->widget_goods_3->show();
        ui->textBrowser_goods_3->setText(QString::number(goods_3.num));

    }
    else
    {
      ui->widget_goods_3->hide();
    }
    if(goods_4.num)
    {
        ui->widget_goods_4->show();
        ui->textBrowser_goods_4->setText(QString::number(goods_4.num));

    }
    else
    {
      ui->widget_goods_4->hide();
    }
    price_all=goods_1.num*goods_1.price+goods_2.num*goods_2.price+goods_3.num*goods_3.price+goods_4.num*goods_4.price;
    ui->textBrowser_price_all->clear();
    ui->textBrowser_price_all->append("总价格");
    ui->textBrowser_price_all->append(QString::number(price_all));
    ui->textBrowser_price_all->append("¥");




}
int MainWindow::getPoint0(Circle c1, Circle c2,Circle c3)
{
    int C_x,C_y,D_x,D_y;
    int x=0,y=0;
    int dist1 = sqrt((c1.get_x() - c2.get_x()) * (c1.get_x() - c2.get_x()) + (c1.get_y() - c2.get_y()) *(c1.get_y() - c2.get_y()));
    //两个圆心的距离
    int dist2 = (c1.get_r() * c1.get_r() - c2.get_r() * c2.get_r() + dist1 * dist1) / (2 * dist1);
    //AE:c1 到两圆交点所在直线的距离
    //E：两圆心所在的直线与两圆交点所在的直线的交点.求E的 xy 坐标
    int E_x = c1.get_x() + (dist2 * (c2.get_x() - c1.get_x())) / dist1;
    int E_y = c1.get_y() + (dist2 * (c2.get_y() - c1.get_y())) / dist1;
    //求两圆心所在的直线的斜率
    int k = -((c1.get_x() - c2.get_x()) / (c1.get_y() - c2.get_y()));
    int CE = c1.get_r() * c1.get_r() - dist2 * dist2;// 1/2 交点所在的直线长度的平方
    //c:两圆交点中一点,求点坐标
    C_x = E_x - sqrt(CE / (1 + k * k));

    C_y = E_y + k * (C_x - E_x);
    D_x = E_x + sqrt(CE / (1 + k * k));
    D_y = E_y + k * (D_x - E_x);
    x=x+C_x+D_x;
    y=y+C_y+D_y;
    if (dist1 > (c1.get_r() + c2.get_r()) || dist1 < abs(c1.get_r() - c2.get_r()))
    {
        return -1;
    }
    //	cout << "("<< C_x << "," << C_y<<")"<< endl;
    //	cout << "("<< D_x << "," << D_y<<")"<< endl;

    dist1 = sqrt((c1.get_x() - c3.get_x()) * (c1.get_x() - c3.get_x()) + (c1.get_y() - c3.get_y()) *(c1.get_y() - c3.get_y()));
    //两个圆心的距离
    dist2 = (c1.get_r() * c1.get_r() - c3.get_r() * c3.get_r() + dist1 * dist1) / (2 * dist1);
    //AE:c1 到两圆交点所在直线的距离
    //E：两圆心所在的直线与两圆交点所在的直线的交点.求E的 xy 坐标
     E_x = c1.get_x() + (dist2 * (c3.get_x() - c1.get_x())) / dist1;
     E_y = c1.get_y() + (dist2 * (c3.get_y() - c1.get_y())) / dist1;
    //求两圆心所在的直线的斜率
     k = -((c1.get_x() - c3.get_x()) / (c1.get_y() - c3.get_y()));
     CE = c1.get_r() * c1.get_r() - dist2 * dist2;// 1/2 交点所在的直线长度的平方
    //c:两圆交点中一点,求点坐标
    C_x = E_x - sqrt(CE / (1 + k * k));
    C_y = E_y + k * (C_x - E_x);
    D_x = E_x + sqrt(CE / (1 + k * k));
    D_y = E_y + k * (D_x - E_x);
    x=x+C_x+D_x;
    y=y+C_y+D_y;
    if (dist1 > (c1.get_r() + c3.get_r()) || dist1 < abs(c1.get_r() - c3.get_r()))
    {

        return -1;
    }
    dist1 = sqrt((c2.get_x() - c3.get_x()) * (c2.get_x() - c3.get_x()) + (c2.get_y() - c3.get_y()) *(c2.get_y() - c3.get_y()));
    //两个圆心的距离
    dist2 = (c2.get_r() * c2.get_r() - c3.get_r() * c3.get_r() + dist1 * dist1) / (2 * dist1);
    //AE:c1 到两圆交点所在直线的距离
    //E：两圆心所在的直线与两圆交点所在的直线的交点.求E的 xy 坐标
     E_x = c2.get_x() + (dist2 * (c3.get_x() - c2.get_x())) / dist1;
     E_y = c2.get_y() + (dist2 * (c3.get_y() - c2.get_y())) / dist1;
    //求两圆心所在的直线的斜率
     k = -((c2.get_x() - c3.get_x()) / (c2.get_y() - c3.get_y()));
     CE = c2.get_r() * c2.get_r() - dist2 * dist2;// 1/2 交点所在的直线长度的平方
    //c:两圆交点中一点,求点坐标
    C_x = E_x - sqrt(CE / (1 + k * k));
    C_y = E_y + k * (C_x - E_x);
    D_x = E_x + sqrt(CE / (1 + k * k));
    D_y = E_y + k * (D_x - E_x);
    x=x+C_x+D_x;
    y=y+C_y+D_y;

    if (dist1 > (c2.get_r() + c3.get_r()) || dist1 < abs(c2.get_r() - c3.get_r()))
    {

        return -1;
    }
    R_x=x/6;
    R_y=y/6;
    return 1;

}
int MainWindow::getPoint1(Circle c1, Circle c2,Circle c3)
{
    int C_x,C_y,D_x,D_y;
    int x=0,y=0;
    int dist1 = sqrt((c1.get_x() - c2.get_x()) * (c1.get_x() - c2.get_x()) + (c1.get_y() - c2.get_y()) *(c1.get_y() - c2.get_y()));
    //两个圆心的距离
    int dist2 = (c1.get_r() * c1.get_r() - c2.get_r() * c2.get_r() + dist1 * dist1) / (2 * dist1);
    //AE:c1 到两圆交点所在直线的距离
    //E：两圆心所在的直线与两圆交点所在的直线的交点.求E的 xy 坐标
    int E_x = c1.get_x() + (dist2 * (c2.get_x() - c1.get_x())) / dist1;
    int E_y = c1.get_y() + (dist2 * (c2.get_y() - c1.get_y())) / dist1;
    //求两圆心所在的直线的斜率
    int k = -((c1.get_x() - c2.get_x()) / (c1.get_y() - c2.get_y()));
    int CE = c1.get_r() * c1.get_r() - dist2 * dist2;// 1/2 交点所在的直线长度的平方
    //c:两圆交点中一点,求点坐标
    C_x = E_x - sqrt(CE / (1 + k * k));
    C_y = E_y + k * (C_x - E_x);
    D_x = E_x + sqrt(CE / (1 + k * k));
    D_y = E_y + k * (D_x - E_x);
    x=x+C_x+D_x;
    y=y+C_y+D_y;
    if (dist1 > (c1.get_r() + c2.get_r()) || dist1 < abs(c1.get_r() - c2.get_r()))
    {
        return -1;
    }
    //	cout << "("<< C_x << "," << C_y<<")"<< endl;
    //	cout << "("<< D_x << "," << D_y<<")"<< endl;

    dist1 = sqrt((c1.get_x() - c3.get_x()) * (c1.get_x() - c3.get_x()) + (c1.get_y() - c3.get_y()) *(c1.get_y() - c3.get_y()));
    //两个圆心的距离
    dist2 = (c1.get_r() * c1.get_r() - c3.get_r() * c3.get_r() + dist1 * dist1) / (2 * dist1);
    //AE:c1 到两圆交点所在直线的距离
    //E：两圆心所在的直线与两圆交点所在的直线的交点.求E的 xy 坐标
     E_x = c1.get_x() + (dist2 * (c3.get_x() - c1.get_x())) / dist1;
     E_y = c1.get_y() + (dist2 * (c3.get_y() - c1.get_y())) / dist1;
    //求两圆心所在的直线的斜率
     k = -((c1.get_x() - c3.get_x()) / (c1.get_y() - c3.get_y()));
     CE = c1.get_r() * c1.get_r() - dist2 * dist2;// 1/2 交点所在的直线长度的平方
    //c:两圆交点中一点,求点坐标
    C_x = E_x - sqrt(CE / (1 + k * k));
    C_y = E_y + k * (C_x - E_x);
    D_x = E_x + sqrt(CE / (1 + k * k));
    D_y = E_y + k * (D_x - E_x);
    x=x+C_x+D_x;
    y=y+C_y+D_y;
    if (dist1 > (c1.get_r() + c3.get_r()) || dist1 < abs(c1.get_r() - c3.get_r()))
    {

        return -1;
    }
    dist1 = sqrt((c2.get_x() - c3.get_x()) * (c2.get_x() - c3.get_x()) + (c2.get_y() - c3.get_y()) *(c2.get_y() - c3.get_y()));
    //两个圆心的距离
    dist2 = (c2.get_r() * c2.get_r() - c3.get_r() * c3.get_r() + dist1 * dist1) / (2 * dist1);
    //AE:c1 到两圆交点所在直线的距离
    //E：两圆心所在的直线与两圆交点所在的直线的交点.求E的 xy 坐标
     E_x = c2.get_x() + (dist2 * (c3.get_x() - c2.get_x())) / dist1;
     E_y = c2.get_y() + (dist2 * (c3.get_y() - c2.get_y())) / dist1;
    //求两圆心所在的直线的斜率
     k = -((c2.get_x() - c3.get_x()) / (c2.get_y() - c3.get_y()));
     CE = c2.get_r() * c2.get_r() - dist2 * dist2;// 1/2 交点所在的直线长度的平方
    //c:两圆交点中一点,求点坐标
    C_x = E_x - sqrt(CE / (1 + k * k));
    C_y = E_y + k * (C_x - E_x);
    D_x = E_x + sqrt(CE / (1 + k * k));
    D_y = E_y + k * (D_x - E_x);
    x=x+C_x+D_x;
    y=y+C_y+D_y;

    if (dist1 > (c2.get_r() + c3.get_r()) || dist1 < abs(c2.get_r() - c3.get_r()))
    {

        return -1;
    }
    R1_x=(x/6+R_x)/2;
    R1_y=(y/6+R_y)/2;
    return 1;

}

void MainWindow::label_clear()
{
    ui->label_goods_1->hide();
    ui->label_goods_2->hide();
    ui->label_goods_3->hide();
    ui->label_goods_4->hide();
}
void MainWindow::on_pushButton_goods_1_clicked()
{
    ui->textBrowser->clear();

    ui->textBrowser->append("饼干 ¥");
    label_clear();
    ui->label_goods_1->show();
     ui->textBrowser->append(QString::number(goods_1.price));
     goods_find_num=1;

}

void MainWindow::on_pushButton_goods_2_clicked()
{
    ui->textBrowser->clear();

    ui->textBrowser->append("湿巾 ¥");
    label_clear();
    ui->label_goods_2->show();
     ui->textBrowser->append(QString::number(goods_2.price));
     goods_find_num=2;
}

void MainWindow::on_pushButton_goods_3_clicked()
{
    ui->textBrowser->clear();

    ui->textBrowser->append("曲奇 ¥");
    label_clear();
    ui->label_goods_3->show();
     ui->textBrowser->append(QString::number(goods_3.price));
     goods_find_num=3;
}

void MainWindow::on_pushButton_goods_4_clicked()
{
    ui->textBrowser->clear();

    ui->textBrowser->append("转接头 ¥");
    label_clear();
    ui->label_goods_4->show();
     ui->textBrowser->append(QString::number(goods_4.price));
     goods_find_num=4;
}




void MainWindow::on_pushButton_deal_all_clicked()
{
    ui->pushButton_pay->show();
}

void MainWindow::on_pushButton_reduce_1_clicked()
{
    if(goods_1.num>=1)
        goods_1.num--;
}

void MainWindow::on_pushButton_reduce_2_clicked()
{
    if(goods_2.num>=1)
        goods_2.num--;
}

void MainWindow::on_pushButton_reduce_3_clicked()
{
    if(goods_3.num>=1)
        goods_3.num--;
}

void MainWindow::on_pushButton_reduce_4_clicked()
{
    if(goods_4.num>=1)
        goods_4.num--;
}

void MainWindow::on_pushButton_deal_1_clicked()
{
    this->on_pushButton_goods_1_clicked();
}

void MainWindow::on_pushButton_deal_2_clicked()
{
    this->on_pushButton_goods_2_clicked();
}

void MainWindow::on_pushButton_deal_3_clicked()
{
    this->on_pushButton_goods_3_clicked();
}

void MainWindow::on_pushButton_deal_4_clicked()
{
    this->on_pushButton_goods_4_clicked();
}

void MainWindow::on_pushButton_start_clicked()
{
    ui->pushButton_start->hide();
}

void MainWindow::on_pushButton_pay_clicked()
{
    navigation_flag=1;
    loc_aim_x=5;
    loc_aim_y=5;
    ui->pushButton_pay->hide();
    ui->pushButton_start->show();
}
